const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/xmuyi').then(() => {
  console.log('数据库连接成功！');
}).catch((err) => {
  console.log('数据库连接失败！');
});

// 用户模板
const UserSchema = new mongoose.Schema({
  username: {// 用户名
    type: String,// 字符串类型
    required: true,// 必填
    unique: true,// 唯一
    trim: true,// 去除首尾空格
    minlength: 3// 最小长度
  },
  password: {// 密码
    type: String,// 字符串类型
    required: true// 必填
  },
});

// 实例化模型
const User = mongoose.model('User', UserSchema);

// 角色模板
const RoleSchema = new mongoose.Schema({
  role_name: {// 角色名称
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  description: {// 角色描述
    type: String,
    trim: true
  },
  created_at: {// 创建时间
    type: Date,
    default: Date.now
  }
});

const Role = mongoose.model('Role', RoleSchema);

// 权限模板
const PermissionSchema = new mongoose.Schema({
  permission_name: { // 权限名称
    type: String,
    required: true,
    unique: true, // 唯一
    trim: true
  },
  description: { // 权限描述
    type: String,
    trim: true
  },
  resource: {// 资源
    type: String,
    required: true,
    trim: true
  },
  action: {// 操作
    type: String,
    required: true,
    trim: true,
    enum: ['create', 'read', 'update', 'delete', 'manage']// create: 创建, read: 读取, update: 更新, delete: 删除, manage: 管理,
  },
  created_at: { // 创建时间
    type: Date,
    default: Date.now
  }
});

const Permission = mongoose.model('Permission', PermissionSchema);

// 用户角色关系模板
const UserRoleSchema = new mongoose.Schema({
  user_id: {// 用户ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  role_id: {// 角色ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Role',
    required: true
  },
  assigned_at: { // 指派时间
    type: Date,
    default: Date.now
  }
});

// 复合唯一索引
UserRoleSchema.index({ user_id: 1, role_id: 1 }, { unique: true });// 联合唯一索引

const UserRole = mongoose.model('UserRole', UserRoleSchema);

// 角色权限关系模板
const RolePermissionSchema = new mongoose.Schema({
  role_id: {// 角色ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Role',
    required: true
  },
  permission_id: {// 权限ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Permission',
    required: true
  },
  assigned_at: { // 指派时间
    type: Date,
    default: Date.now
  }
});

// 复合唯一索引（角色与权限的组合唯一）
RolePermissionSchema.index({ role_id: 1, permission_id: 1 }, { unique: true });

const RolePermission = mongoose.model('RolePermission', RolePermissionSchema);

// 用户权限关系模板
const UserPermissionSchema = new mongoose.Schema({
  user_id: {// 用户ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  permission_id: {// 权限ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Permission',
    required: true
  },
  assigned_at: { // 指派时间
    type: Date,
    default: Date.now
  }
});

// 复合唯一索引
UserPermissionSchema.index({ user_id: 1, permission_id: 1 }, { unique: true });// 联合唯一索引

const UserPermission = mongoose.model('UserPermission', UserPermissionSchema);

// 课程模板
const CourseSchema = new mongoose.Schema({
  course_name: {// 课程名称
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  description: {// 课程描述
    type: String,
    trim: true
  },
  created_at: {// 创建时间
    type: Date,
    default: Date.now
  }
});

const Course = mongoose.model('Course', CourseSchema);
// 学生列表模板
const StudentListSchema = new mongoose.Schema({
  student_name: {// 学生姓名
    type: String,
    required: true,
    trim: true
  },
  student_id: {// 学生ID
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  course_id: {// 课程ID
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Course',
    required: true
  },
  created_at: {// 创建时间
    type: Date,
    default: Date.now
  }
});

const StudentList = mongoose.model('StudentList', StudentListSchema);

module.exports = { UserRole, Permission, User, Role, UserPermission, RolePermission, Course, StudentList }; // 补充导出RolePermission
